package com.lts.controller;


import com.alibaba.fastjson.JSONObject;
import com.lts.entity.User;
import com.lts.mapper.UserMapper;
import com.lts.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 爆炸杰克
 * @since 2021-09-08
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserMapper userMapper;

    @ResponseBody
    @PostMapping("/Login")
    private String getUser(@RequestBody JSONObject jsonObject){
        System.out.println(jsonObject.getString("username"));

        String username=jsonObject.getString("username");

        String password=jsonObject.getString("password");

        Map<String,Object> map=new HashMap<String,Object>();

        map.put("username",username);

        map.put("password",password);

        List<User> user=userMapper.selectByMap(map);

        if(user.size()==1){
            System.out.println(map.get("username"));
            System.out.println("非空");
            return "登录成功";
        }
        else {
            System.out.println(map.get("username"));
            System.out.println("空");
            return "没有该用户";
        }
    }

    @ResponseBody
    @PostMapping("/Register")
    public String Register(@RequestBody JSONObject jsonObject){
        String username=jsonObject.getString("username");
        String password1=jsonObject.getString("password1");
        String password2=jsonObject.getString("password2");

        if(!password1.equals(password2)){
            return "重复密码不正确";
        }

        Map<String,Object> map=new HashMap<>();

        map.put("username",username);

        map.put("password",password1);

        List<User> user=userMapper.selectByMap(map);

        if(user.size()==0){
            User user1=new User();

            user1.setUsername(username);

            user1.setPassword(password1);
            int i=userMapper.insert(user1);

            if(i==1){
                return "注册成功";
            }
            else {
                return "注册失败";
            }
        }
        else {
            return "已存在该用户";
        }
    }

}
